package edu.mcw.rgd.clinminer.domain.view;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.EntityManager;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.eclipse.persistence.annotations.ReadOnly;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import edu.mcw.rgd.clinminer.domain.Participant;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
@Table(name = "ALL_ANNOTATIONS")
@ReadOnly
@NamedQueries({
		@NamedQuery(name = "Annotation.getDocumentValues", query = "SELECT new edu.mcw.rgd.clinminer.domain.view."
				+ "JPQLDocumentSuggestionResult(view.documentSource, COUNT(view.documentSource)) "
				+ "FROM Annotation view GROUP BY view.documentSource ORDER BY COUNT(view.documentSource) DESC"),
		@NamedQuery(name = "Annotation.getOntologyValues", query = "SELECT new edu.mcw.rgd.clinminer.domain.view."
				+ "JPQLOntologySuggestionResult(view.ontologyTermLabel, view.ontologyTermID, COUNT(view.ontologyTermLabel)) "
				+ "FROM Annotation view WHERE view.ontologyTermLabel IS NOT NULL "
				+ "GROUP BY view.ontologyTermLabel,view.ontologyTermID ORDER BY COUNT(view.ontologyTermLabel) DESC"),
		@NamedQuery(name = "Annotation.getLOINCValues", query = "SELECT new edu.mcw.rgd.clinminer.domain.view."
				+ "JPQLOntologySuggestionResult(view.LOINCLabel, view.LOINCID, COUNT(view.LOINCLabel)) "
				+ "FROM Annotation view WHERE view.LOINCLabel IS NOT NULL "
				+ "GROUP BY view.LOINCLabel,view.LOINCID ORDER BY COUNT(view.LOINCLabel) DESC") })
public class Annotation {
	@EmbeddedId
	private AnnotationCompositeKey id;

	@ManyToOne
	@JoinColumn(name = "participant")
	private Participant participant;

	@Column(name = "participant_name")
	private String participantName;

	@Column(name = "value")
	private String value;

	@Column(name = "unit")
	private String unit;

	@Column(name = "tested_phenotype_label")
	private String testedPhenotypeLabel;

	//
	// Recordable
	//

	@Column(name = "annotated_text")
	private String annotatedText;

	@Column(name = "document_source")
	private String documentSource;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "S-")
	@Column(name = "document_source_date")
	private Date documentSourceDate;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "S-")
	@Column(name = "event_date")
	private Date eventDate;

	@Column(name = "ontology_term_label")
	private String ontologyTermLabel;

	@Column(name = "ontology_termid")
	private String ontologyTermID;

	@Column(name = "notes")
	private String notes;

	//
	// Measurable
	//
	@Column(name = "diagnostics_loinc_id")
	private String LOINCID;

	@Column(name = "diagnostics_loinc_label")
	private String LOINCLabel;

	public static List<JPQLDocumentSuggestionResult> getDocumentComboValues() {
		List<JPQLDocumentSuggestionResult> result = entityManager()
				.createNamedQuery("Annotation.getDocumentValues",
						JPQLDocumentSuggestionResult.class).getResultList();
		return result;
	}

	public static List<JPQLOntologySuggestionResult> getOntologyValues() {
		List<JPQLOntologySuggestionResult> result = entityManager()
				.createNamedQuery("Annotation.getOntologyValues",
						JPQLOntologySuggestionResult.class).getResultList();
		return result;
	}

	public static List<JPQLOntologySuggestionResult> getLOINCValues() {
		List<JPQLOntologySuggestionResult> result = entityManager()
				.createNamedQuery("Annotation.getLOINCValues",
						JPQLOntologySuggestionResult.class).getResultList();
		return result;
	}

	@PersistenceContext(unitName = "persistenceUnit")
	transient EntityManager entityManager;
}
